package com.xingzhi.shortvideosharingplatform.mapper;

import com.xingzhi.shortvideosharingplatform.entity.Video;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xingzhi.shortvideosharingplatform.vo.VideoVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 视频信息表 Mapper 接口
 * </p>
 *
 * @author zjw
 * @since 2025-06-17
 */
@Mapper
public interface VideoMapper extends BaseMapper<Video> {
    /**
     * 查询所有视频列表（带分页）
     * @param offset 偏移量
     * @param pageSize 每页大小
     * @return 视频列表
     */
    @Select("SELECT v.*, u.user_name as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.status = 1 " +  // 只查询已发布的视频
            "ORDER BY v.created_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<VideoVO> selectAllVideos(@Param("offset") Integer offset,
                                  @Param("pageSize") Integer pageSize);

    /**
     * 查询所有视频总数（用于分页）
     * @return 视频总数
     */
    @Select("SELECT COUNT(*) FROM video WHERE status = 2")
    Integer selectAllVideoCount();

    /**
     * 根据视频ID查询视频详情
     */
    @Select("SELECT v.*, u.nickname as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.id = #{videoId}")
    VideoVO selectVideoDetailById(@Param("videoId") Long videoId);

    /**
     * 查询用户发布的视频列表
     */
    @Select("SELECT v.*, u.nickname as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.user_id = #{userId} AND v.status = 2 " +
            "ORDER BY v.created_time DESC")
    List<VideoVO> selectVideosByUserId(@Param("userId") Long userId);

    /**
     * 查询分类下的视频列表
     */
    @Select("SELECT v.*, u.nickname as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.category_id = #{categoryId} AND v.status = 2 " +
            "ORDER BY v.created_time DESC")
    List<VideoVO> selectVideosByCategoryId(@Param("categoryId") Integer categoryId);

    /**
     * 分页查询推荐视频列表
     */
    @Select("SELECT v.*, u.nickname as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.status = 2 " +
            "ORDER BY v.view_count DESC, v.created_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<VideoVO> selectRecommendVideos(@Param("offset") Integer offset,
                                        @Param("pageSize") Integer pageSize);

    /**
     * 搜索视频
     */
    @Select("SELECT v.*, u.nickname as author " +
            "FROM video v " +
            "LEFT JOIN users u ON v.user_id = u.id " +
            "WHERE v.status = 2 AND (MATCH(v.title, v.description) AGAINST(#{keyword} IN NATURAL LANGUAGE MODE)) " +
            "ORDER BY v.created_time DESC")
    List<VideoVO> searchVideos(@Param("keyword") String keyword);

}
